package user

import (
	"context"
	"errors"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	rpc "gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserModifyPasswordLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员管理 - 密码修改
func NewUserModifyPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserModifyPasswordLogic {
	return &UserModifyPasswordLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserModifyPasswordLogic) UserModifyPassword(req *types.UserModifyPasswordRequest) (resp *types.UserModifyPasswordResponse, err error) {
	// 对传递参数做简单逻辑校验
	if req.UserId == 0 {
		err := errors.New("id is empty")
		logx.Errorf("Call UserModifyPassword error: %v", err)
		return nil, err
	}

	if req.NewPassword == "" {
		err := errors.New("new password is empty")
		logx.Errorf("Call UserModifyPassword error: %v", err)
		return nil, err
	}

	// 组合请求结构体
	rpcReq := &rpc.UserModifyPasswordRequest{
		UserId:      req.UserId,
		Nickname:    req.Nickname,
		Account:     req.Account,
		NewPassword: req.NewPassword,
	}

	// 发送请求
	_, err = l.svcCtx.UserServiceRPC.UserModifyPassword(l.ctx, rpcReq)
	if err != nil {
		logx.Errorf("Call UserModifyPassword error: %v", err)
		return nil, err
	}

	// 处理请求返回的结果
	// 解析、组合数据
	resp = &types.UserModifyPasswordResponse{}
	return
}
